package com.elbis.business.controller.account;

import java.util.Date;
import java.util.Locale;

import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;

import org.apache.log4j.Logger;

import com.elbis.action.input.ErrorMessages;
import com.elbis.action.input.ErrorsHashMap;
import com.elbis.action.output.InformativeMessages;
import com.elbis.business.controller.msg.FormFields;
import com.elbis.business.entity.Gamer;
import com.elbis.business.entity.PendingActivation;
import com.elbis.business.manager.ActivationManager;
import com.elbis.business.manager.ManageGamer;
import com.elbis.business.manager.MessageManager;
import com.elbis.business.util.ErrorMessageUtil;

public class ActivateAccountController {

	private static final Logger logger = Logger
			.getLogger(ActivateAccountController.class);

	private String email = null;

	private String token = null;

	private String userPreferredLanguage = null;

	public void activate(ComponentSystemEvent event) {

		if (userPreferredLanguage == null) {
			userPreferredLanguage = ErrorMessages.DEFAULT_MESSAGE_LANGUAGE;
		}

		if (email == null || email.isEmpty() || token == null
				|| token.isEmpty()) {
			ErrorMessageUtil.addErrorMessage(
					FormFields.ACTIVATION_RESPONSE,
					MessageManager.getErrorMessage(
							ErrorMessages.WRONG_ACTIVATION_VALUES,
							userPreferredLanguage).getMessage());
			logger.error(ErrorsHashMap
					.getMessage(ErrorMessages.WRONG_ACTIVATION_VALUES));
			return;
		}

		ManageGamer mg = new ManageGamer();

		Gamer noob = mg.getUniqueUser(email);

		// check if there is such an entry with provided email address
		if (noob == null) {
			ErrorMessageUtil.addErrorMessage(
					FormFields.ACTIVATION_RESPONSE,
					MessageManager.getErrorMessage(
							ErrorMessages.WRONG_ACTIVATION_VALUES,
							userPreferredLanguage).getMessage());
			logger.error(ErrorsHashMap
					.getMessage(ErrorMessages.WRONG_ACTIVATION_VALUES));
			return;
		}

		// check if it already activated the account
		if (noob.isActive()) {
			ErrorMessageUtil.addErrorMessage(
					FormFields.ACTIVATION_RESPONSE,
					MessageManager.getErrorMessage(
							ErrorMessages.ACCOUNT_ALREADY_ACTIVATED,
							userPreferredLanguage).getMessage());
			logger.error(ErrorsHashMap
					.getMessage(ErrorMessages.ACCOUNT_ALREADY_ACTIVATED));
			return;
		}

		ActivationManager activationManager = new ActivationManager();

		PendingActivation pending = activationManager.getForUser(noob);

		if (pending == null) {
			ErrorMessageUtil.addErrorMessage(
					FormFields.ACTIVATION_RESPONSE,
					MessageManager.getErrorMessage(
							ErrorMessages.WRONG_ACTIVATION_VALUES,
							userPreferredLanguage).getMessage());
			logger.error(ErrorsHashMap
					.getMessage(ErrorMessages.WRONG_ACTIVATION_VALUES));
			return;
		} else {
			// check if the provided token is valid
			if (!token.equals(pending.getToken())) {
				ErrorMessageUtil.addErrorMessage(
						FormFields.ACTIVATION_RESPONSE,
						MessageManager.getErrorMessage(
								ErrorMessages.WRONG_ACTIVATION_VALUES,
								userPreferredLanguage).getMessage());
				logger.error(ErrorsHashMap
						.getMessage(ErrorMessages.WRONG_ACTIVATION_VALUES));
				return;
			}
			// check if the activation link has expired
			Date now = new Date(System.currentTimeMillis());
			if (now.after(pending.getExpires())) {
				ErrorMessageUtil.addErrorMessage(
						FormFields.ACTIVATION_RESPONSE,
						MessageManager.getErrorMessage(
								ErrorMessages.ACTIVATION_LINK_EXPIRED,
								userPreferredLanguage).getMessage());
				logger.error(ErrorsHashMap
						.getMessage(ErrorMessages.ACTIVATION_LINK_EXPIRED));
				return;
			} else {
				// activation successful
				noob.setActive(true);
				mg.update(noob);
				activationManager.delete(pending);
				ErrorMessageUtil.addErrorMessage(
						FormFields.ACTIVATION_RESPONSE,
						MessageManager.getInformativeMessage(
								InformativeMessages.ACCOUNT_ACTIVATED,
								userPreferredLanguage).getMessage());
				FacesContext
						.getCurrentInstance()
						.getViewRoot()
						.setLocale(Locale.forLanguageTag(userPreferredLanguage));
			}
		}
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getUserPreferredLanguage() {
		return userPreferredLanguage;
	}

	public void setUserPreferredLanguage(String lang) {
		this.userPreferredLanguage = lang;
	}

}
